package com.spring.shops.service.impl;

import com.spring.shops.dao.TzOrderItemMapper;
import com.spring.shops.dao.TzOrderMapper;
import com.spring.shops.dao.TzShopDetailMapper;
import com.spring.shops.dao.TzUserAddrOrderMapper;
import com.spring.shops.model.dto.OrderPageDTO;
import com.spring.shops.model.entity.TzOrder;
import com.spring.shops.model.entity.TzOrderItem;
import com.spring.shops.model.entity.TzShopDetail;
import com.spring.shops.model.entity.TzUserAddrOrder;
import com.spring.shops.model.vo.ExportExcelVo;
import com.spring.shops.model.vo.OrderRecordsVo;
import com.spring.shops.model.vo.OrderVo;
import com.spring.shops.model.vo.OrderWaitingVo;
import com.spring.shops.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private TzOrderMapper tzOrderMapper;

    @Resource
    private TzShopDetailMapper tzShopDetailMapper;

    @Resource
    private TzOrderItemMapper tzOrderItemMapper;

    @Resource
    private TzUserAddrOrderMapper tzUserAddrOrderMapper;

    @Override
    public OrderVo orderPageSearch(OrderVo orderVo, OrderPageDTO orderPageDTO) {
        orderPageDTO.setCurrent(orderVo.getCurrent());
        orderPageDTO.setSize(orderVo.getSize());
        List<TzOrder> tzOrders = tzOrderMapper.selectByPageSearch(orderPageDTO);
        int total = tzOrders.size();
        orderVo.setTotal(total);
        List<OrderRecordsVo> list = new ArrayList<>();
        for (TzOrder order : tzOrders) {
            OrderRecordsVo orderRecordsVo = new OrderRecordsVo(order.getOrderId(), order.getShopId(), order.getProdName(), order.getUserId(), order.getOrderNumber(), order.getTotal(), order.getActualTotal(), order.getPayType(), order.getRemarks(), order.getStatus(), order.getDvyType(), order.getDvyId(), order.getDvyFlowId(), order.getFreightAmount(), order.getAddrOrderId(), order.getProductNums(), order.getCreateTime(), order.getUpdateTime(), order.getPayTime(), order.getDvyTime(), order.getFinallyTime(), order.getCancelTime(), order.getIsPayed(), order.getDeleteStatus(), order.getRefundSts(), order.getReduceAmount());
            TzOrderItem tzOrderItem = tzOrderItemMapper.selectByPrimaryKey(order.getOrderId());
            List<TzOrderItem> li = new ArrayList<>();
            li.add(tzOrderItem);
            orderRecordsVo.setOrderItems(li);
            TzUserAddrOrder tzUserAddrOrder = tzUserAddrOrderMapper.selectByPrimaryKey(order.getAddrOrderId());
            orderRecordsVo.setUserAddrOrder(tzUserAddrOrder);
            list.add(orderRecordsVo);
        }
        orderVo.setRecords(list);
        return orderVo;
    }

    public OrderRecordsVo info(String id) {
        TzOrder order = tzOrderMapper.selectByOrderNumber(Long.valueOf(id));
        OrderRecordsVo orderRecordsVo = new OrderRecordsVo(order.getOrderId(), order.getShopId(), order.getProdName(), order.getUserId(), order.getOrderNumber(), order.getTotal(), order.getActualTotal(), order.getPayType(), order.getRemarks(), order.getStatus(), order.getDvyType(), order.getDvyId(), order.getDvyFlowId(), order.getFreightAmount(), order.getAddrOrderId(), order.getProductNums(), order.getCreateTime(), order.getUpdateTime(), order.getPayTime(), order.getDvyTime(), order.getFinallyTime(), order.getCancelTime(), order.getIsPayed(), order.getDeleteStatus(), order.getRefundSts(), order.getReduceAmount());
        TzOrderItem tzOrderItem = tzOrderItemMapper.selectByPrimaryKey(order.getOrderId());
        List<TzOrderItem> li = new ArrayList<>();
        li.add(tzOrderItem);
        orderRecordsVo.setOrderItems(li);

        TzUserAddrOrder tzUserAddrOrder = tzUserAddrOrderMapper.selectByPrimaryKey(order.getAddrOrderId());
        orderRecordsVo.setUserAddrOrder(tzUserAddrOrder);
        return orderRecordsVo;
    }

    @Override
    public List<ExportExcelVo> waitExcel(String consignmentName, String consignmentMobile, String consignmentAddr) {
        List<Integer> shopIds = tzOrderMapper.selectByStatus();
        OrderWaitingVo orderWaitingVo = new OrderWaitingVo(consignmentName, consignmentMobile, consignmentAddr, shopIds);
        TzShopDetail tzShopDetail = tzShopDetailMapper.selectByDetail(orderWaitingVo);

        String s;
        if (tzShopDetail!=null) {
            s = tzShopDetail.getProvince() + tzShopDetail.getCity() + tzShopDetail.getArea() + tzShopDetail.getShopAddress();
            List<ExportExcelVo> list = new ArrayList<>();
            List<TzOrder> tzOrders = tzOrderMapper.selectByShopId(tzShopDetail.getShopId());
            //提取userid，in查询，返回userlist
            //userlist转换成map，key userId, value User对象

            List<String> listUserIds=new ArrayList<>();
            for (TzOrder tzOrder : tzOrders){
                listUserIds.add(tzOrder.getUserId());
            }
            List<TzUserAddrOrder> tzUserAddrOrders = tzUserAddrOrderMapper.selectByUserId(listUserIds);
            Map<String,TzUserAddrOrder> map=new HashMap<>();
            for (TzUserAddrOrder tzUserAddrOrder: tzUserAddrOrders){
                map.put(tzUserAddrOrder.getUserId(),tzUserAddrOrder);
            }
            for (TzOrder tzOrder : tzOrders) {
                ExportExcelVo exportExcelVo = new ExportExcelVo();
                exportExcelVo.setShopOwnerName(tzShopDetail.getShopOwner());
                exportExcelVo.setShopAddr(s);
                exportExcelVo.setShopOwnerMobile(tzShopDetail.getMobile());

                exportExcelVo.setOrderId(tzOrder.getOrderId());
                exportExcelVo.setMsg(tzOrder.getRemarks());
                exportExcelVo.setShopsName(tzOrder.getProdName());
                exportExcelVo.setShopsCount(Long.valueOf(tzOrder.getProductNums()));

                TzUserAddrOrder tzUserAddrOrder = map.get(tzOrder.getUserId());
                exportExcelVo.setUserMobile(tzUserAddrOrder.getMobile());
                String s1 = tzUserAddrOrder.getProvince() + tzUserAddrOrder.getCity() + tzUserAddrOrder.getArea() + tzUserAddrOrder.getAddr();
                exportExcelVo.setUserAddr(s);
                exportExcelVo.setUserName(tzUserAddrOrder.getReceiver());
                list.add(exportExcelVo);
            }
            return list;
        }else {
            List<ExportExcelVo> list= new ArrayList<>();
            return list;
        }

    }
}
